package org.leaf.green.b2b

class StockInController {

    def stockInService

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : "/b2b/stockIn/list", model : [stockInInstanceList: StockIn.list(params), stockInInstanceTotal: StockIn.count()]
    }

    def create = {
        def stockInInstance = stockInService.init(params) //new StockIn()

        def type = stockInService.type(stockInInstance?.recordType?.id)
        render view : "/b2b/stockIn/${type}/create", model : [stockInInstance: stockInInstance]
    }

    def save = {
        def stockInInstance = new StockIn(params)
        if (!stockInInstance.hasErrors() && stockInInstance.save()) {
            flash.message = "stockIn.created"
            flash.args = [stockInInstance.id]
            flash.defaultMessage = "StockIn ${stockInInstance.id} created"
            redirect(action: "show", id: stockInInstance.id)
        }
        else {
            def type = stockInService.type(stockInInstance?.recordType?.id)
            render(view: "/b2b/stockIn/${type}/create", model: [stockInInstance: stockInInstance])
        }
    }

    def show = {
        def stockInInstance = StockIn.get(params.id)
        if (!stockInInstance) {
            flash.message = "stockIn.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "StockIn not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            def type = stockInService.type(stockInInstance?.recordType?.id)
            render view : "/b2b/stockIn/${type}/show", model : [stockInInstance: stockInInstance]
        }
    }

    def edit = {
        def stockInInstance = StockIn.get(params.id)
        if (!stockInInstance) {
            flash.message = "stockIn.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "StockIn not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            def type = stockInService.type(stockInInstance?.recordType?.id)
            render view : "/b2b/stockIn/${type}/edit", model : [stockInInstance: stockInInstance]
        }
    }

    def update = {
        def stockInInstance = StockIn.get(params.id)
        if (stockInInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (stockInInstance.version > version) {
                    stockInInstance.errors.rejectValue("version", "stockIn.optimistic.locking.failure", "Another user has updated this StockIn while you were editing")
                    def type = stockInService.type(stockInInstance?.recordType?.id)
                    render(view: "/b2b/stockIn/${type}/edit", model: [stockInInstance: stockInInstance])
                    return
                }
            }
            stockInInstance.properties = params
            if (!stockInInstance.hasErrors() && stockInInstance.save(flush:true)) {
                flash.message = "stockIn.updated"
                flash.args = [params.id]
                flash.defaultMessage = "StockIn ${params.id} updated"
                redirect(action: "show", id: stockInInstance.id)
            }
            else {
                def type = stockInService.type(stockInInstance?.recordType?.id)
                render(view: "/b2b/stockIn/${type}/edit", model: [stockInInstance: stockInInstance])
            }
        }
        else {
            flash.message = "stockIn.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "StockIn not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def stockInInstance = StockIn.get(params.id)
        if (stockInInstance) {
            try {
                stockInInstance.delete()
                flash.message = "stockIn.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "StockIn ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "stockIn.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "StockIn ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "stockIn.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "StockIn not found with id ${params.id}"
            redirect(action: "list")
        }
    }

    def approval = {
        def approval = stockInService.approval(params.int('id').toLong())
        if(approval){
            flash.message = "stockIn.approval.successful"
            flash.args = [params.id]
            flash.defaultMessage = "StockIn ${params.id} approved successful"
            redirect(action: "show", id: params.id)
        }else{
            flash.message = "stockIn.approval.failed"
            flash.args = [params.id]
            flash.defaultMessage = "StockIn ${params.id} approval failed"
            redirect(action: "show", id: params.id)
        }
    }

}
